package com.gurid.service.ucenter;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.sql.Types;

@SpringBootTest
public class CodeGenerator {
    @Test
    public void generate(){
        String projectPath = System.getProperty("user.dir");
        String outputPath = projectPath + "/src/main/java";

        FastAutoGenerator.create("jdbc:mysql://192.168.100.131:3306/guli_ucenter?serverTimezone=GMT%2B8",
                        "root",
                        "Gurid:MySqlPass1")
                .globalConfig(builder -> {
                    builder.author("Gurid") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            .enableSpringdoc() // 开启swagger3模式
                            .dateType(DateType.ONLY_DATE) //时间策略
                            .commentDate("yyyy-MM-dd") //注释日期
                            .disableOpenDir()
                            //.fileOverride() // 覆盖已生成文件
                            .outputDir(outputPath); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    if (typeCode == Types.TINYINT) {
                        return DbColumnType.BOOLEAN;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.gurid.service") // 设置父包名
                            .moduleName("ucenter") // 设置父包模块名
                            .controller("controller")
                            .entity("entity")
                            .service("service")
                            .mapper("mapper")
                            //.pathInfo(Collections.singletonMap(OutputFile.xml, outputPath)); // 设置mapperXml生成路径
                            ;
                })
                .strategyConfig(builder -> {
                    builder.addInclude("ucenter_member") // 设置需要生成的表名
                            .addTablePrefix("edu_", "t_", "c_", "ucenter_") // 设置过滤表前缀
                            .enableCapitalMode()
                            //Entity策略
                            .entityBuilder()
                            .enableLombok()
                            .enableChainModel()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .logicDeleteColumnName("is_deleted")
                            //.logicDeletePropertyName("is_deleted")
                            .enableRemoveIsPrefix() //去掉boolean值的is_前缀
                            .addTableFills(new Column("gmt_create", FieldFill.INSERT)) // 自动填充配置
                            .addTableFills(new Column("gmt_modified", FieldFill.INSERT_UPDATE))
                            .idType(IdType.ASSIGN_ID)
                            .formatFileName("%s")
                            .superClass("com.gurid.service.base.model.BaseEntity") // 设置baseEntity
                            .addSuperEntityColumns("id", "gmt_create", "gmt_modified") //设置公共字段
                            .enableFileOverride() // 覆盖已生成文件
                            //Controller策略
                            .controllerBuilder()
                            .enableRestStyle() //开启@RestController
                            .enableHyphenStyle() //url中驼峰转换为连字符  www.xxx.com/asdEva转换为www.xxx.com/asd-eva
                            .formatFileName("%sController") //Controller文件名称
                            .enableFileOverride() // 覆盖已生成文件
                            //Service策略
                            .serviceBuilder()
                            .formatServiceFileName("%sService") //Service文件名称
                            .formatServiceImplFileName("%sServiceImpl") //ServiceImpl文件名称
                            .enableFileOverride() // 覆盖已生成文件
                            //Mapper策略
                            .mapperBuilder()
                            .formatMapperFileName("%sMapper")
                            .disableMapperXml()
                            .enableFileOverride() // 覆盖已生成文件
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                //.templateEngine(new EnjoyTemplateEngine())
                .execute();
    }
}
